/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.adv.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkTraditionAdvReward;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkTraditionAdvRewardDao;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkTraditionAdvGoods;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkTraditionAdvGoodsDao;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkTraditionAdvIntegral;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkTraditionAdvIntegralDao;

/**
 * 传统文化配置Service
 * @author 罗天文
 * @version 2018-03-20
 */
@Service
@Transactional(readOnly = true)
public class HkTraditionAdvRewardService extends CrudService<HkTraditionAdvRewardDao, HkTraditionAdvReward> {

	@Autowired
	private HkTraditionAdvGoodsDao hkTraditionAdvGoodsDao;
	@Autowired
	private HkTraditionAdvIntegralDao hkTraditionAdvIntegralDao;
	
	public HkTraditionAdvReward get(String id) {
		HkTraditionAdvReward hkTraditionAdvReward = super.get(id);
		hkTraditionAdvReward.setHkTraditionAdvGoodsList(hkTraditionAdvGoodsDao.findList(new HkTraditionAdvGoods(hkTraditionAdvReward)));
		hkTraditionAdvReward.setHkTraditionAdvIntegralList(hkTraditionAdvIntegralDao.findList(new HkTraditionAdvIntegral(hkTraditionAdvReward)));
		return hkTraditionAdvReward;
	}
	
	public List<HkTraditionAdvReward> findList(HkTraditionAdvReward hkTraditionAdvReward) {
		return super.findList(hkTraditionAdvReward);
	}
	
	public Page<HkTraditionAdvReward> findPage(Page<HkTraditionAdvReward> page, HkTraditionAdvReward hkTraditionAdvReward) {
		return super.findPage(page, hkTraditionAdvReward);
	}
	
	@Transactional(readOnly = false)
	public void save(HkTraditionAdvReward hkTraditionAdvReward) {
		super.save(hkTraditionAdvReward);
		for (HkTraditionAdvGoods hkTraditionAdvGoods : hkTraditionAdvReward.getHkTraditionAdvGoodsList()){
			if (hkTraditionAdvGoods.getId() == null){
				continue;
			}
			if (HkTraditionAdvGoods.DEL_FLAG_NORMAL.equals(hkTraditionAdvGoods.getDelFlag())){
				hkTraditionAdvGoods.setCityId(hkTraditionAdvReward.getCity().getId());
				hkTraditionAdvGoods.setCountryId(hkTraditionAdvReward.getCountry().getId());
				hkTraditionAdvGoods.setProvinceId(hkTraditionAdvReward.getProvince().getId());
				hkTraditionAdvGoods.setEndDate(hkTraditionAdvReward.getEndDate());
				hkTraditionAdvGoods.setBeginDate(hkTraditionAdvReward.getBeginDate());
				hkTraditionAdvGoods.setTradition(hkTraditionAdvReward.getTradition());
				if (StringUtils.isBlank(hkTraditionAdvGoods.getId())){
					hkTraditionAdvGoods.setRewardId(hkTraditionAdvReward);
					hkTraditionAdvGoods.preInsert();
					hkTraditionAdvGoodsDao.insert(hkTraditionAdvGoods);
				}else{
					hkTraditionAdvGoods.preUpdate();
					hkTraditionAdvGoodsDao.update(hkTraditionAdvGoods);
				}
			}else{
				hkTraditionAdvGoodsDao.delete(hkTraditionAdvGoods);
			}
		}
		for (HkTraditionAdvIntegral hkTraditionAdvIntegral : hkTraditionAdvReward.getHkTraditionAdvIntegralList()){
			if (hkTraditionAdvIntegral.getId() == null){
				continue;
			}
			if (HkTraditionAdvIntegral.DEL_FLAG_NORMAL.equals(hkTraditionAdvIntegral.getDelFlag())){
				hkTraditionAdvIntegral.setCityId(hkTraditionAdvReward.getCity().getId());
				hkTraditionAdvIntegral.setCountryId(hkTraditionAdvReward.getCountry().getId());
				hkTraditionAdvIntegral.setProvinceId(hkTraditionAdvReward.getProvince().getId());
				hkTraditionAdvIntegral.setEndDate(hkTraditionAdvReward.getEndDate());
				hkTraditionAdvIntegral.setBeginDate(hkTraditionAdvReward.getBeginDate());
				hkTraditionAdvIntegral.setTradition(hkTraditionAdvReward.getTradition());
				if (StringUtils.isBlank(hkTraditionAdvIntegral.getId())){
					hkTraditionAdvIntegral.setReward(hkTraditionAdvReward);
					hkTraditionAdvIntegral.preInsert();
					hkTraditionAdvIntegralDao.insert(hkTraditionAdvIntegral);
				}else{
					hkTraditionAdvIntegral.preUpdate();
					hkTraditionAdvIntegralDao.update(hkTraditionAdvIntegral);
				}
			}else{
				hkTraditionAdvIntegralDao.delete(hkTraditionAdvIntegral);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkTraditionAdvReward hkTraditionAdvReward) {
		super.delete(hkTraditionAdvReward);
		hkTraditionAdvGoodsDao.delete(new HkTraditionAdvGoods(hkTraditionAdvReward));
		hkTraditionAdvIntegralDao.delete(new HkTraditionAdvIntegral(hkTraditionAdvReward));
	}
	
}